import sys
if sys.platform != 'win32':
    import fcntl
import socket
import struct

# 获取指定网卡的ip  
def get_ip_unix(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])


def get_local_ip():
    hostName = socket.getfqdn(socket.gethostname())
    return socket.gethostbyname(hostName)

if sys.platform != 'win32':
    ip = get_ip_unix('eth0')
else:
    ip = get_local_ip()


print(ip)